package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.IDifferentiateCategoryService;
import com.mdd.admin.validate.DifferentiateCategoryCreateValidate;
import com.mdd.admin.validate.DifferentiateCategoryUpdateValidate;
import com.mdd.admin.validate.DifferentiateCategorySearchValidate;
import com.mdd.admin.vo.DifferentiateCategoryListedVo;
import com.mdd.admin.vo.DifferentiateCategoryDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.DifferentiateCategory;
import com.mdd.common.mapper.DifferentiateCategoryMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 【请填写功能名称】实现类
 * @author LikeAdmin
 */
@Service
public class DifferentiateCategoryServiceImpl implements IDifferentiateCategoryService {
        
    @Resource
    DifferentiateCategoryMapper differentiateCategoryMapper;

    /**
     * 【请填写功能名称】列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<DifferentiateCategoryListedVo>
     */
    @Override
    public PageResult<DifferentiateCategoryListedVo> list(PageValidate pageValidate, DifferentiateCategorySearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<DifferentiateCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        differentiateCategoryMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "like:categoryName@category_name:str",
            "=:status:str",
            "=:createId@create_id:int",
            "like:createName@create_name:str",
            "=:type:str",
        });

        IPage<DifferentiateCategory> iPage = differentiateCategoryMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<DifferentiateCategoryListedVo> list = new LinkedList<>();
        for(DifferentiateCategory item : iPage.getRecords()) {
            DifferentiateCategoryListedVo vo = new DifferentiateCategoryListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime().getTime()));
            vo.setUpdateTime(TimeUtils.timestampToDate(item.getUpdateTime().getTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 【请填写功能名称】详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return DifferentiateCategory
     */
    @Override
    public DifferentiateCategoryDetailVo detail(String id) {
        DifferentiateCategory model = differentiateCategoryMapper.selectOne(
                new QueryWrapper<DifferentiateCategory>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        DifferentiateCategoryDetailVo vo = new DifferentiateCategoryDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 【请填写功能名称】新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(DifferentiateCategoryCreateValidate createValidate) {
        DifferentiateCategory model = new DifferentiateCategory();
        model.setCategoryName(createValidate.getCategoryName());
        model.setStatus(createValidate.getStatus());
        model.setCreateTime(new Date());
        model.setUpdateTime(new Date());
        model.setCreateId(createValidate.getCreateId());
        model.setCreateName(createValidate.getCreateName());
        model.setType(createValidate.getType());
        differentiateCategoryMapper.insert(model);
    }

    /**
     * 【请填写功能名称】编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(DifferentiateCategoryUpdateValidate updateValidate) {
        DifferentiateCategory model = differentiateCategoryMapper.selectOne(
                new QueryWrapper<DifferentiateCategory>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setCategoryName(updateValidate.getCategoryName());
        model.setStatus(updateValidate.getStatus());
        model.setUpdateTime(new Date());
        model.setCreateId(updateValidate.getCreateId());
        model.setCreateName(updateValidate.getCreateName());
        model.setType(updateValidate.getType());
        differentiateCategoryMapper.updateById(model);
    }

    /**
     * 【请填写功能名称】删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(String id) {
        DifferentiateCategory model = differentiateCategoryMapper.selectOne(
                new QueryWrapper<DifferentiateCategory>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        differentiateCategoryMapper.delete(new QueryWrapper<DifferentiateCategory>().eq("id", id));
    }

}
